//
//  MainDetaalViewController.m
//  ALFashion
//
//  Created by dengzhicheng on 15/6/16.
//  Copyright (c) 2015年 dengzhicheng. All rights reserved.
//

#import "MainDetailViewController.h"
#import <AFNetworking.h>
#import "MainDetailModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MainDetailViewController ()

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) MainDetailModel *model;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, assign) NSInteger height;
@property (nonatomic, retain) UIScrollView *scroll;
@end

@implementation MainDetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT)];
    [self.view addSubview:self.scroll];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 30, 40, 30)];
    [backButton setBackgroundColor:[UIColor lightGrayColor]];
    [backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];

    [self startConnect];
}

- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)startConnect
{
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", self.docid];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dic = [bigDic objectForKey:self.docid];
        self.model = [[MainDetailModel alloc] init];
        self.model.str = [dic objectForKeyedSubscript:@"body"];
        self.model.array = [dic objectForKeyedSubscript:@"img"];
        
        
        NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        self.rect = [[self filterHTML:self.model.str]  boundingRectWithSize:CGSizeMake(WIDTH - 40, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];
        
        [self createView];
         [self.scroll setContentSize:CGSizeMake(0, self.rect.size.height + 0.1 * HEIGHT  + self.height)];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)createView
{
    
    NSArray *imageArray = self.model.array;
    if (imageArray.count) {
        self.height = 0.1 * HEIGHT + 64;
        for (int i = 0; i < imageArray.count; i++) {
            NSDictionary *dic = [imageArray objectAtIndex:i];
            NSString *a = [dic objectForKey:@"pixel"];
            NSArray *sizeArray = [a componentsSeparatedByString:@"*"];
            NSInteger last = [[sizeArray lastObject] integerValue];
            NSInteger first = [[sizeArray firstObject] integerValue];
            CGFloat size = (last * 1.0 / first * 1.0);
            if ([a length] != 0) {
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.027 * WIDTH, self.height, WIDTH - (0.027 * 2 * WIDTH), size * WIDTH - (0.027 * 2 * WIDTH))];
                [imageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"src"]] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
                self.height += imageView.frame.size.height + 10;
                
                [self.scroll addSubview:imageView];
            }
        }
        UILabel *noteLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.027 * WIDTH, self.height, WIDTH - (0.027 * 2 * WIDTH), self.rect.size.height)];
        noteLabel.numberOfLines = 0;
        noteLabel.text = [self filterHTML:self.model.str];
        [self.scroll addSubview:noteLabel];
        
        
        
        
    }else{
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 40, self.rect.size.height)];
        label.text = [self filterHTML:self.model.str];;
        label.numberOfLines = 0;
        [self.scroll addSubview:label];
    }

    
    
}

#pragma mark -
#pragma mark 去掉HTML标签
- (NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = @"p";
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@"\n"];
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    NSLog(@"%@", html);
    return html;
}














@end
